package models;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Formula;

import service.ICommonService;
import utils.StringHelper;
import config.Constant;

@SuppressWarnings("serial")
@Entity
@Table(name="sys_module")
public class Module extends IdEntity{

	@Formula("(select COUNT(*) from sys_module m where m.parent_id = id and m.if_func ='false')")
    private int subModuleCount;
	
	@Formula("(select COUNT(*) from sys_module f where f.parent_id = id and f.if_func ='true')")   
	private int subFunctionCount;
	
	@Transient
	private Integer parentId;
	
	private String name;
	private String description;
	private Integer position;
	private String active;
	private String code;
	private String ifFunc;
	private String funcUrl;
	
	@Column(length=1000)
	private String devName;
	
	@ManyToOne
	private Module parent;


	@Transient
	private List<Module> children;
	
	
	public Module getParent() {
		return parent;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getPosition() {
		return position;
	}

	public void setPosition(Integer position) {
		this.position = position;
	}

	public void setParent(Module parent) {
		this.parent = parent;
	}

	public String getActive() {
		return active;
	}

	public void setActive(String active) {
		this.active = active;
	}
	
	public int getSubFunctionCount() {
		return subFunctionCount;
	}

	public void setSubFunctionCount(int subFunctionCount) {
		this.subFunctionCount = subFunctionCount;
	}

	public int getSubModuleCount() {
		return subModuleCount;
	}

	public void setSubModuleCount(int subModuleCount) {
		this.subModuleCount = subModuleCount;
	}

	public Integer getParentId() {
		return parentId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}

	public String getIfFunc() {
		return ifFunc;
	}

	public void setIfFunc(String ifFunc) {
		this.ifFunc = ifFunc;
	}

	public String getFuncUrl() {
		return funcUrl;
	}

	public void setFuncUrl(String funcUrl) {
		this.funcUrl = funcUrl;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getDevName() {
		if(StringUtils.isEmpty(devName)) {
			
			if(StringHelper.parseBoolean(ifFunc)){
				return "N/A";
			}
			
			return "-";
			
		}
		return devName;
	}

	public void setDevName(String devName) {
		this.devName = devName;
	}

	@SuppressWarnings("unchecked")
	public List<Module> getChildren() {
		ICommonService commonService = (ICommonService)Constant.AppContext.getBean("commonService");
		if(children==null){
			children = commonService.findAll("from Module m where m.parent.id = ? order by m.position asc", id);
		}
		return children;
	}

	public void setChildren(List<Module> children) {
		this.children = children;
	}
	

	
}
